<?php
/**
 * 此文件是接口程序路由
 *
 * @package class
 * @author Jerry
 * @version $Id: application.class.php 6317 2009-11-06 08:28:37Z rox $
 */


/**
 * 程序路由处理类
 *
 * 这里类判断外界参数调用内部方法
 */
class Application
{
	var $controller 		= null;  		//类名
	var $action				= null;  		//方法名
	var $default_controller = null;   		//默认的类名
	var $default_do         = null;     	//默认的方法名
	var $controller_var     = 'c';  		//Post or Get 控制器变量
	var $action_var         = 'd';			//Post or Get 函数变量
	var $sub_dir            = '';			//控制器子目录

	/**
	 * 设置默认的类名
	 *
	 * @param string $default_controller
	 */
	function setDefaultController($default_controller)
	{
		$this->default_controller = $default_controller;
	}

	/**
	 * 设置默认的方法名
	 *
	 * @param string $default_action
	 */
	function setDefaultAction($default_action)
	{
		$this->default_action = $default_action;
	}

	
	/**
	 * 设置控制器子目录
	 *
	 * @param string $dir
	 */
	function setSubDir($dir)
	{
		$this->sub_dir = $dir;
	}
	
	/**
	 * 
	*过滤器
	*
	*/
	function filter()
	{
		global $sys_conf_var,$in_var;
		
		#记录目标页面，等待验证通过后跳转过去
		$_SESSION['pay']['url_prev'] = get_url($this->controller,$this->action);
		
		if(count($sys_conf_var['filter']) > 0)
		{
			if($sys_conf_var['filter_global']['isGlobal'])
			{
				$sys_conf_var['filter_global']['function']($in_var);
			}
			
			foreach($sys_conf_var['filter'] as $key => $var)
			{
				$sub_dir = $this->sub_dir ? $this->sub_dir . '/' : '';
				
				if($this->sub_dir == $var['dir'] && strpos(CONTROLLER_DIR . $sub_dir ,'/' . $var['dir']))
				{
					if(!empty($var['controller']) && preg_match("/".$var['controller']."/i",$this->controller))
					{
						if( !empty($var['rule']['actions'])&& count($var['rule']['actions']) > 0 )
						{
							foreach($var['rule']['actions'] as $key => $item)
							{
								if(preg_match("/".$item."/i",$this->action))
								{
									$var['rule']['function']($in_var);
								}
							}
						}
						else
						{
							$var['rule']['function']($in_var);
						}
					}
				}
			}
		}
	}

	//运行自动加载的控制器
	function atuorun()
	{
		global $sys_conf_var,$in_var;

		//运行自动加载的控制器开始
		if(count($sys_conf_var['atuorun']) > 0)
		{
			foreach ($sys_conf_var['atuorun'] as $key => $var)
			{
				$sub_dir = $this->sub_dir ? $this->sub_dir . '/' : '';

				if($this->sub_dir == $var['dir'] && strpos(CONTROLLER_DIR . $sub_dir ,'/' . $var['dir']))
				{
					$class_file = CONTROLLER_DIR . $sub_dir . $var['controller'] . '.class.php';

					if(!is_file($class_file))
			        {
			        	throw new AppException('Invalid controller ' . $var['controller']);
			        }

			        require_once $class_file;

			        $class = ucfirst($var['controller']);

					if(!class_exists($class))
					{
						throw new AppException('Invalid Class ' . $class);
					}

					$instance = new $class();

					$function = $var['function'];

					if(!method_exists($instance,$function))
					{
						throw new AppException('Invalid Function ' . $function);
					}
					
					$instance->tpl->assign('controller', $var['controller']);
					$instance->tpl->assign('action', $var['function']);
					
					$instance->$function($this->controller,$this->action);
				}
			}
		}

		//运行自动加载的控制器结束
	}

	/**
	 * 调用实际类和方式
	 *
	 */
	function run()
	{
		global $in_var;

		$this->controller = $in_var[$this->controller_var] ? $in_var[$this->controller_var] : $this->default_controller;
		$this->action     = $in_var[$this->action_var] ? $in_var[$this->action_var] : $this->default_action;
		$this->controller = preg_replace('/[\.\/]/i','',$this->controller);
		
		$this->atuorun();
		$this->filter();
		
		if(!$this->controller || !$this->action) throw new AppException('The lack of parameters');

		$sub_dir = $this->sub_dir ? $this->sub_dir . '/' : '';

		$class_file = CONTROLLER_DIR . $sub_dir . $this->controller . '.class.php';

		if(!is_file($class_file))
        {
        	throw new AppException('Invalid controller ' . $this->controller);
        }

		require_once $class_file;

		$class = ucfirst($this->controller);

		if(!class_exists($class))
		{
			throw new AppException('Invalid Class ' . $class);
		}

		$instance = new $class();

		$function = $this->action;

		if(!method_exists($instance,$function))
		{
			throw new AppException('Invalid Function ' . $function);
		}

		$instance->tpl->assign('controller', $this->controller);
		$instance->tpl->assign('action', $function);
		
		$instance->$function();
	}
}


/**
 * $Log$
 */
?>